package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.SysMenuDTO;
import com.andy.blog.admin.dto.TreeSelectDTO;
import com.andy.blog.admin.service.SysMenuBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.dal.vo.SysMenuVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 菜单信息
 * Created by andy on 2023-04-05 11:38
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController {

    @Resource
    private SysMenuBusService sysMenuBusService;

    /**
     * 获取菜单列表
     */
    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    @GetMapping("/list")
    public GeneralResponseResult<List<SysMenuDTO>> list(SysMenuVO sysMenuVO) {
        return GeneralResponseResult.success(sysMenuBusService.selectMenuList(sysMenuVO));
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    @GetMapping(value = "/{id}")
    public GeneralResponseResult<SysMenuDTO> getInfo(@PathVariable Long id) {
        return GeneralResponseResult.success(sysMenuBusService.selectMenuById(id));
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public GeneralResponseResult<List<TreeSelectDTO>> buildMenuTreeSelect(SysMenuVO sysMenuVO) {
        return GeneralResponseResult.success(sysMenuBusService.buildMenuTreeSelect(sysMenuVO));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public GeneralResponseResult<Map<String, Object>> roleMenuTreeSelect(@PathVariable("roleId") Long roleId) {
        return GeneralResponseResult.success(sysMenuBusService.roleMenuTreeSelect(roleId));
    }

    /**
     * 新增菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:add')")
    @Log(title = "菜单管理", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    public GeneralResponseResult<String> add(@Validated @RequestBody SysMenuVO sysMenuVO) {
        sysMenuBusService.add(sysMenuVO);
        return GeneralResponseResult.success();
    }

    /**
     * 修改菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @Log(title = "菜单管理", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    public GeneralResponseResult<String> edit(@Validated @RequestBody SysMenuVO sysMenuVO) {
        sysMenuBusService.edit(sysMenuVO);
        return GeneralResponseResult.success();
    }

    /**
     * 删除菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:remove')")
    @Log(title = "菜单管理", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{menuId}")
    public GeneralResponseResult<Void> remove(@PathVariable("menuId") Long menuId) {
        sysMenuBusService.removeMenuById(menuId);
        return GeneralResponseResult.success();
    }
}